package the_tenth_chapter;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

public class ReadFile {

	public static void main(String[] args) {
		RandomAccessFile randomFile;
		try {
			// r：表示以只读方式打开文件
			randomFile = new RandomAccessFile("C:/Users/Administrator/Desktop/a.txt", "r");
			System.out.println("文件的长度为：" + randomFile.length());
			System.out.println("文件位置指针为 :" + randomFile.getFilePointer());

			String s;
			while ((s = randomFile.readLine()) != null) {
				System.out.println(s);
			}
			long filePointer = 0, fileLength = randomFile.length();
			// 移动文件指针到文件头
			randomFile.seek(0);

			int i = 1;
			while (i < fileLength) {
				s = randomFile.readLine();
				System.out.println((i++) + " " + s);
				// 文件指针位置 到文件尾为7
				filePointer = randomFile.getFilePointer();
				System.out.println(filePointer);
				randomFile.seek(0);
			}
			randomFile.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
